package com.dmp.onlinestore.model.dao.base;

import com.dmp.onlinestore.model.dao.ProductDao;
import com.dmp.onlinestore.model.dao.jdbc.*;
import com.dmp.onlinestore.model.dao.jdbc.base.JdbcDaoTransaction;

import com.dmp.onlinestore.model.dao.testmemdb.base.InMemDaoTransaction;
import com.dmp.onlinestore.model.dao.testmemdb.InMemProductDao;

public class DaoFactory {

	private static int DB_TYPE_XML            = 0;
	private static int DB_TYPE_JDBC           = 1;
	private static int DB_TYPE_TEST_IN_MEM_DB = 2; // A test, in-memory, "database" (very limited).
	
	//private static int DB_TYPE_ACTIVE = DB_TYPE_JDBC;
	private static int DB_TYPE_ACTIVE = DB_TYPE_TEST_IN_MEM_DB;
	
	private static DaoFactory instance = null;
	
	// Hide the constructor.
	private DaoFactory () {
	}
	
	public static DaoFactory instance() {
		if (instance == null) {
			instance = new DaoFactory();
		}
		return instance;
	}
	
	public DaoTransaction getDaoTransaction() throws DaoException {
	    if (DB_TYPE_ACTIVE == DB_TYPE_JDBC) {
	    	return new JdbcDaoTransaction();
	    }
	    else if (DB_TYPE_ACTIVE == DB_TYPE_TEST_IN_MEM_DB){
	    	return new InMemDaoTransaction();
	    }
	    else {
	    	return null;
	    }
	}

	public ProductDao getProductDao(DaoTransaction transaction) {
	    if (DB_TYPE_ACTIVE == DB_TYPE_JDBC) {
	    	return new JdbcProductDao(transaction);
	    }
	    else if (DB_TYPE_ACTIVE == DB_TYPE_XML) {
	        return null;//new XmlProductDao();
	    }
	    else if (DB_TYPE_ACTIVE == DB_TYPE_TEST_IN_MEM_DB) {
	    	return new InMemProductDao(transaction);
	    }
	    else {
	    	return null;
	    }
	}
	
}
